package com.hcq.controller;

import com.alibaba.fastjson.JSON;
import com.common.annotation.RequireRoles;
import com.common.entity.PageParam;
import com.common.entity.Rsp;
import com.common.entity.Scenic;
import com.common.entity.User;
import com.common.util.JsonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hcq.json.Sen;
import com.hcq.service.ScenicServiceImpl;
import io.swagger.annotations.ApiOperation;
//import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
public class ScenicController {

    @Autowired
    ScenicServiceImpl scenicService;

    @PostMapping("/searchSomeScenicByPage")
    @RequireRoles(roles = {"admin","super_admin"},index = 2)
    @ApiOperation("查询所有用户 支持 多条件 分页 排序 查询")
    public Rsp searchSomeByPage(@RequestBody PageParam<Scenic> scenicPageParam,
                                @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){

        System.out.println(scenicPageParam);

        return Rsp.success(scenicService.searchSomeByPage(scenicPageParam));
    }

    @PostMapping("/getOneById")
    @RequireRoles(roles = {"admin","super_admin"},index = 2)
    @ApiOperation("查询所有用户 支持 多条件 分页 排序 查询")
    public Rsp getOneById(@RequestBody Scenic scenic,
                          @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){
        scenic.setCreateUserId(userId);
        return Rsp.success(scenicService.getOneById(scenic.getId()));
    }

    @PostMapping("/addOneScenic")
    @RequireRoles(roles = {"admin","super_admin"},index = 2)
    public Rsp addOneScenic(@RequestBody Scenic scenic,
                            @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){
        scenic.setCreateUserId(userId);
        return scenicService.addOne(scenic);
    }

    @PostMapping("/updateScenic")
    @RequireRoles(roles = {"admin","super_admin"},index = 2)
    public Rsp updateScenic(@RequestBody Scenic scenic,
                          @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){
        return scenicService.update(scenic);
    }

    @PostMapping("/deleteScenicLogical")
    @RequireRoles(roles = {"admin","super_admin"},index = 2)
    public Rsp deleteScenicLogical(@RequestBody Scenic scenic,
                            @RequestHeader(value = "userId") String userId, @RequestHeader(value = "roleSign") String roleSign){
        return scenicService.deleteLogical(scenic);
    }

    @PostMapping("/findHotScenic")
    public Rsp findHotScenic(){
        return scenicService.findHotScenic();
    }


//    @GetMapping("/test")
//    public String test(@RequestHeader(value = "userId") String userId) throws IOException {
//        List<Scenic> scenics = new ArrayList<>(100);
//        List<Sen> all = new ArrayList<>(100);
//
//        for (int i = 1; i < 15; i++) {
//            String s = JsonUtil.readJsonFile("E:/全国景区 - 副本 ("+i+").txt");
//
//            //拿到data数据
//            JSONObject jsonObject = JSONObject.fromObject(s);
//            String data = JSON.toJSONString(jsonObject.get("data"));
//
//            //拿到record数据
//            JSONObject jsonObject2 = JSONObject.fromObject(data);
//            String record = JSON.toJSONString(jsonObject2.get("record"));
//
//            //转为list集合
//            ObjectMapper mapper = new ObjectMapper();
//            List<Sen> sens = mapper.readValue(record, new TypeReference<List<Sen>>() {
//            });
//            //山东：1376 14  湖北 1710 1 湖南 1828 2 上海802  3  深圳1965  4
//            //重庆2324 6  四川2368 7 云南2671 8 浙江934 9 黑龙江656 10
//            // 香港3717 11 澳门3739 12 西藏2817 13 新疆3207 5
//            for(Sen sen : sens){
//                if(i == 1)
//                    sen.setAreaId("1710");
//                if(i == 2)
//                    sen.setAreaId("1828");
//                if(i == 3)
//                    sen.setAreaId("802");
//                if(i == 4)
//                    sen.setAreaId("1965");
//                if(i == 5)
//                    sen.setAreaId("3207");
//                if(i == 6)
//                    sen.setAreaId("2324");
//                if(i == 7)
//                    sen.setAreaId("2368");
//                if(i == 8)
//                    sen.setAreaId("2671");
//                if(i == 9)
//                    sen.setAreaId("934");
//                if(i == 10)
//                    sen.setAreaId("656");
//                if(i == 11)
//                    sen.setAreaId("3717");
//                if(i == 12)
//                    sen.setAreaId("3739");
//                if(i == 13)
//                    sen.setAreaId("2817");
//                if(i == 14)
//                    sen.setAreaId("1376");
//            }
//            all.addAll(sens);
//        }
//
//        for (Sen s : all){
//            Scenic scenic = new Scenic();
//            scenic.setName(s.getSpot());
//            scenic.setGrade(s.getGrade().length());
//            scenic.setLng(new BigDecimal(s.getLng()));
//            scenic.setLat(new BigDecimal(s.getLat()));
//            scenic.setAddress(s.getAddr());
//            scenic.setVisitTime(s.getVisittime());
//            scenic.setType(s.getType());
//            scenic.setOpenTime(s.getOpentime());
//            scenic.setContactPhone(s.getTel());
//            scenic.setUrl(s.getUrl());
//            scenic.setAreaId(s.getAreaId());
//            scenic.setCreateUserId(userId);
//            scenics.add(scenic);
//        }
//        return scenicService.test(scenics);
//    }
}
